package com.koi.satoken.config;

import cn.dev33.satoken.listener.SaTokenListener;
import cn.dev33.satoken.stp.SaLoginModel;
import com.koi.common.constants.KoiConstants;
import com.koi.plus.mapper.system.SysLoginUserMapper;
import com.koi.satoken.service.loginlog.SaLoginLogService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @author ･ᴗ･
 * @description [自定义侦听器的实现]
 */
@Slf4j
@RequiredArgsConstructor
@Component
public class KoiSaTokenListener implements SaTokenListener {

    private final SysLoginUserMapper sysLoginUserMapper;

    private final SaLoginLogService saLoginLogService;

    /**
     * @description [每次登录时触发]
     */
    @Override
    public void doLogin(String loginType, Object loginId, String tokenValue, SaLoginModel loginModel) {
        log.info("自定义侦听器实现-doLogin");
        log.info("登录类型：{}, 登录ID：{}, Token值：{}, 登录Model：{}", loginType, loginId, tokenValue, loginModel);
        // 保存登录日志
        // saLoginLogService.addLoginLog(sysLoginUserMapper.selectById(Long.valueOf(String.valueOf(loginId))).getLoginName(), KoiConstants.ZERO_STRING, "登录成功");
    }

    /**
     * @description [每次注销时触发]
     */
    @Override
    public void doLogout(String loginType, Object loginId, String tokenValue) {
        log.info("自定义侦听器实现 doLogout");
        // 保存注销日志
        saLoginLogService.addLoginLog(sysLoginUserMapper.selectById(Long.valueOf(String.valueOf(loginId))).getLoginName(), KoiConstants.ZERO_STRING, "退出登录");
    }

    /**
     * @description [每次被踢下线时触发]
     */
    @Override
    public void doKickout(String loginType, Object loginId, String tokenValue) {
        log.info("自定义侦听器实现 doKickout");
        // 保存踢下线日志
        saLoginLogService.addLoginLog(sysLoginUserMapper.selectById(Long.valueOf(String.valueOf(loginId))).getLoginName(), KoiConstants.ZERO_STRING, "强退下线");
    }

    /**
     * @description [每次被顶下线时触发]
     */
    @Override
    public void doReplaced(String loginType, Object loginId, String tokenValue) {
        log.info("自定义侦听器实现 doReplaced");
        // 保存被顶下线日志
        saLoginLogService.addLoginLog(sysLoginUserMapper.selectById(Long.valueOf(String.valueOf(loginId))).getLoginName(), KoiConstants.ZERO_STRING, "被顶下线");
    }

    /**
     * @description [每次被封禁时触发]
     */
    @Override
    public void doDisable(String loginType, Object loginId, String service, int level, long disableTime) {
        log.info("自定义侦听器实现 doDisable");
        // 保存被封禁时日志
        saLoginLogService.addLoginLog(sysLoginUserMapper.selectById(Long.valueOf(String.valueOf(loginId))).getLoginName(), KoiConstants.ZERO_STRING, "账号被封禁");
    }

    /**
     * @description [每次被解封时触发]
     */
    @Override
    public void doUntieDisable(String loginType, Object loginId, String service) {
        log.info("自定义侦听器实现 doUntieDisable");
        // 保存被解封时日志
        saLoginLogService.addLoginLog(sysLoginUserMapper.selectById(Long.valueOf(String.valueOf(loginId))).getLoginName(), "0", "账号被解封");
    }

    /**
     * @description [每次二级认证时触发]
     */
    @Override
    public void doOpenSafe(String loginType, String tokenValue, String service, long safeTime) {
        log.info("自定义侦听器实现 doOpenSafe");
    }

    /**
     * @description [每次退出二级认证时触发]
     */
    @Override
    public void doCloseSafe(String loginType, String tokenValue, String service) {
        log.info("自定义侦听器实现 doCloseSafe");
    }

    /**
     * @description [每次创建Session时触发]
     */
    @Override
    public void doCreateSession(String id) {
        log.info("自定义侦听器实现 doCreateSession");
    }

    /**
     * @description [每次注销Session时触发]
     */
    @Override
    public void doLogoutSession(String id) {
        log.info("自定义侦听器实现 doLogoutSession");
    }

    /**
     * @description [每次Token续期时触发]
     */
    @Override
    public void doRenewTimeout(String tokenValue, Object loginId, long timeout) {
        log.info("自定义侦听器实现 doRenewTimeout");
    }

}
